package com.zy;

import com.zy.api.HostInfo;
import com.zy.util.HostInfoCollector;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @program: monitor147
 * @description:
 * @author: zy
 * @create: 2025-06-04 18:56
 */
public class MonitorClientHandler  extends SimpleChannelInboundHandler<Object> {
    private static final Logger logger= LoggerFactory.getLogger(MonitorClientHandler.class);

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("收到服务端消息："+msg);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //只要通道一建立，就定时向服务器发送本机 状态信息
        Channel ch=ctx.channel();
        ch.eventLoop().scheduleAtFixedRate(
                ()->{
                    HostInfo hi= null;
                    try {
                        hi = HostInfoCollector.getHostInfo();
                    } catch (InterruptedException e) {
                        logger.error("oshi获取本机信息失败,原因:{}", e.getMessage());
                    }
                    ch.writeAndFlush(hi);
                    logger.info("调度一次发送任务，数据:{}", hi);
                } , 5, 5, java.util.concurrent.TimeUnit.SECONDS
        );
        super.channelActive(ctx);
    }
}
